package toubiao.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javassist.expr.NewArray;

import org.apache.bcel.generic.NEW;
import org.docx4j.model.datastorage.XPathEnhancerParser.locationPath_return;
import org.docx4j.model.datastorage.XPathEnhancerParser.primaryExpr_return;
import org.docx4j.model.datastorage.XPathEnhancerParser.step_return;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.BaseDaoI;
import toubiao.dao.business.ContractDaoI;
import toubiao.dao.business.ContractPhotoDaoI;
import toubiao.dao.business.ProjectDaoI;
import toubiao.domain.po.business.PoContract;
import toubiao.domain.po.business.PoContractPhoto;
import toubiao.domain.po.business.PoPhoto;
import toubiao.domain.po.business.PoProject;
import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.business.ContractPhoto;
import toubiao.domain.vo.business.Photo;
import toubiao.exception.CustomException;
import toubiao.factory.FactoryDao;
import toubiao.utils.ProUtilLogger;
import toubiao.utils.ProUtilPoConvert;
import toubiao.utils.ProUtilVoConvert;
import utils.UtilBean;
import utils.UtilCollection;
import utils.UtilString;

@Service
public class PhotoServiceImpl {
	
	
	private Logger logger=ProUtilLogger.getRootLogger();
	
	@Autowired
	private ProUtilVoConvert voConvert;
	
	
	@Autowired
	private ProjectDaoI projectDao;
	@Autowired
	private ContractDaoI contractDao;
	@Autowired
	private ContractPhotoDaoI contractPhotoDao;
	
	
	

	public Json getContractPhotoListOfProjectByProjectId(String projectId) throws InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		Json json=new Json();
		logger.info("project = "+projectId);
		List<ContractPhoto> contractPhotoList=new ArrayList<>();
		if(UtilString.isNullOrEmpty(projectId)){
			json.setMessage("projectId is null or empty");
			return json;
		}
		
		Long projectIdLong=Long.parseLong(projectId);
		PoProject project=projectDao.get(PoProject.class, projectIdLong);
		if(project==null){
			json.setMessage("Id=projectId 的project 找不到");
			return json;
		}
		
		Set<PoContract> contractSet=project.getContractSet();
		if(UtilCollection.isNullOrEmpty(contractSet)){
			json.setSuccess(true);
			json.setMessage("contractSet is null or empty");
			json.setData(contractPhotoList);
			return json;
		}
		for(PoContract contract:contractSet){
			if(contract!=null){
				Set<PoContractPhoto> set=contract.getContractPhotoSet();
				List<ContractPhoto> list=ProUtilPoConvert.getList(PoContractPhoto.class, ContractPhoto.class, set);
				if(!UtilCollection.isNullOrEmpty(set)){
					contractPhotoList.addAll(list);
				}
			}
		}
		json.setSuccess(true);
		json.setData(contractPhotoList);
		
		return json;
	}


	public Json getPhotoById(String photoType, String photoId) throws InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		Json json=new Json();
		if(UtilString.isNullOrEmpty(photoId,photoType)){
			json.setMessage("photoTyp or photoId is null or empty.");
			return json;
		}
		
		photoType=photoType.trim().toLowerCase();
		
		
		if(photoType.equalsIgnoreCase("contractPhoto")){
			PoContractPhoto poContractPhoto=contractPhotoDao.get(PoContractPhoto.class, Long.parseLong(photoId));
			ContractPhoto contractPhoto=ProUtilPoConvert.convertRoute(PoContractPhoto.class, ContractPhoto.class, poContractPhoto);
			json.setData(contractPhoto);
			json.setSuccess(true);
			return json;
		}
	
		
		json.setMessage("photoType 不正确");
		return json;
	}

	
	    /**
	     * @throws IllegalAccessException 
	     * @throws InstantiationException 
	     * @throws CustomException 
	    * @Title: operatePhoto
	    * @Description: 根据opertorType,对photo 进行操作,当操作为Update时,需要所有相关字段
	    * @param @param opertorType
	    * @param @param photo
	    * @param @return    
	    * @return Json    
	    * @throws
	    */
	public Json operatePhoto(String opertorType, Photo photo) throws CustomException, InstantiationException, IllegalAccessException{
		String[] fildeNames=UtilBean.getFiledNameArray(photo);
		return operatePhoto(opertorType,photo,fildeNames);
	}
	

	
	    /**
	     * @throws IllegalAccessException 
	     * @throws InstantiationException 
	    * @Title: operatePhoto
	    * @Description: 根据opertorType,对photo 进行操作,当操作为Update时, operateFieldNames 为需要更新的字段
	    * @param @param opertorType
	    * @param @param photo
	    * @param @param operateFieldNames  当操作类型为update时: operateFieldNames 为需要更新的字段
	    * @param @return
	    * @param @throws CustomException    
	    * @return Json    
	    * @throws
	    */
	public Json operatePhoto(String opertorType, Photo photo,String[] operateFieldNames) throws CustomException, InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		Json json=new Json();
		if(UtilString.isNullOrEmpty(opertorType)){
			json.setMessage("opertorType is null or empty.");
			return json;
		}
		if(photo==null){
			json.setMessage("photo is null.");
			return json;
		}
		
		BaseDaoI dao=null;
		PoPhoto databasePo=null;//最终入库的po
		Photo databaseVo=null;
		Class voClass=photo.getClass();
		Class poClass=null;
		
		//根据 voClass 类型确定 po以及 dao的类型
		if(voClass==ContractPhoto.class){
			dao=contractPhotoDao;
			poClass=PoContractPhoto.class;
		}
		
		
		//PoPhoto poWeb=voConvert.convert(srcClass, targetClass, photo);
		
		if(dao==null){
			json.setMessage("未定义对应Photo类型的 dao.");
			return json;
		}
		
		opertorType=opertorType.trim().toLowerCase();
		Long id=photo.getId();
		if(opertorType.equals("update") || opertorType.equals("delete") ){
			if(id==null){
				throw new CustomException("进行update或delete 操作, 而 id 未空.");
			}
		}
		
		//update operation
		if(opertorType.equals("update")){
			databasePo=(PoPhoto) dao.get(poClass, id);
			databaseVo=ProUtilPoConvert.convertRoute(poClass, voClass, databasePo);
			
			UtilBean.copyPropertiesByFieldNames(photo, databaseVo, operateFieldNames);
			databasePo=voConvert.convert(voClass, poClass, databaseVo);
			dao.update(databasePo);
		}
		
		//delete operation
		if(opertorType.equals("delete")){
			databasePo=(PoPhoto) dao.get(poClass, id);
			dao.delete(databasePo);
		}
		
		json.setSuccess(true);
		json.setData(databaseVo);
		
		return json;
	}

}
